/**
 *
 * The MIT License
 *
 * Copyright (c) 2008 Board of Trustees, Leland Stanford Jr. University. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.macdadi.multi.client.components;

import com.google.gwt.user.client.ui.*;

/**
 * @author Ben Suter <ben.suter@gmail.com>
 */
public class SearchProjectsPanel extends Composite
{
    public static final String CSS_CLASS = "macdadi-SearchProjectsPanel";

    protected TextBox query;
    protected CheckBox recentOnly;
    protected CheckBox membershipOnly;
    protected Button search;
    protected HTML feedback;

    protected FilteredProjectListing filteredProjectListing;

    public SearchProjectsPanel(FilteredProjectListing filteredProjectListing)
    {
        this.filteredProjectListing = filteredProjectListing;

        VerticalPanel vp = new VerticalPanel();
        // no heading for the moment

        query = new TextBox();
        recentOnly = new CheckBox();
        membershipOnly = new CheckBox();
        search = new Button("Search");
        search.addClickListener(new ClickListener() {
            public void onClick(Widget sender)
            {
                doSearchProjects();
            }
        });
        feedback = new HTML();

        FlexTable table = new FlexTable();
        table.setWidget(0, 0, new HTML("Keywords"));
        table.setWidget(0, 1, query);
        table.setWidget(0, 2, new HTML("e.g. stanford"));
        table.setWidget(1, 0, new HTML("Only recent?"));
        table.setWidget(1, 1, recentOnly);
        table.setWidget(2, 0, new HTML("Only yours?"));
        table.setWidget(2, 1, membershipOnly);
        table.setWidget(3, 0, search);
        table.getFlexCellFormatter().setColSpan(3, 0, 3);
        table.setWidget(4, 0, feedback);
        table.getFlexCellFormatter().setColSpan(4, 0, 3);
        //table.getFlexCellFormatter().setHorizontalAlignment(3, 0, HasAlignment.ALIGN_CENTER);
        vp.add(table);

        initWidget(vp);
        setStyleName(CSS_CLASS);
    }

    protected void doSearchProjects()
    {
        feedback.setText("");

        String queryVal;
        queryVal = query.getText().trim();

        String keywords[];
        /* Split by white space. Originally used [\\s]* but that doesn't work right, also tried [ ]* but no good either. */
        keywords = queryVal.split(" "); // TODO: eliminate empty matches here!

        if ( keywords.length > 1 ) {
            feedback.setText("Please limit your query to a single keyword (we aim to improve this)");
            return;
        }
                        
        boolean recentOnlyVal, membershipOnlyVal;
        recentOnlyVal = recentOnly.isChecked();
        membershipOnlyVal = membershipOnly.isChecked();

        filteredProjectListing.listProjects(keywords, recentOnlyVal, membershipOnlyVal);
    }

    protected void doFeedback(String msg)
    {
        feedback.setText(msg);
    }
}